home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / motif / ohidden.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.0 KB  |  352 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * ohidden.c : openGL (motif) example showing how to use stencils
  20.  *             to draw outlined polygons.
  21.  *
  22.  * Author : Yusuf Attarwala
  23.  *          SGI - Applications
  24.  * Date   : Jul 93
  25.  *
  26.  *    note : the main intent of this program is to demo the stencil
  27.  *           plane functionality, hence the rendering is kept
  28.  *           simple (wireframe).
  29.  *
  30.  *    press  left   button for animation
  31.  *    press  middle button to turn OFF stenciling
  32.  *    press  right  button to turn ON  stenciling
  33.  *
  34.  *---------------------------------------------------------------------------*/
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38.  
  39. #include <Xm/Xm.h> 
  40. #include <Xm/Frame.h>               /* for frame widgets */
  41. #include <Xm/Form.h>               /* for frame widgets */
  42. #include <X11/keysym.h>             /* keyboard translations */
  43. #include <X11/StringDefs.h>
  44.  
  45. #include <GL/gl.h>                  /* gl includes */
  46. #include <GL/glu.h>                 /* utility library includes */
  47. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  48.  
  49. static int stencilOn = 1;
  50.  
  51. /* function declarations */
  52.  
  53. void 
  54.     createToplevel(void),
  55.     drawScene(void),
  56.     setMatrix(void),
  57.     animation(void),
  58.     exposeCB(Widget,XtPointer,XtPointer),
  59.     resizeCB(Widget,XtPointer,XtPointer),
  60.     initCB(Widget,XtPointer,XtPointer),
  61.     inputCB(Widget,XtPointer,XtPointer),
  62.     drawWireframe(int face),
  63.     drawFilled(int face);
  64.  
  65. /* global variables */
  66.             
  67. Display       *display;             /* current display */
  68. XtAppContext  appContext;           /* X application context */
  69. float         ax,ay,az;             /* angles for animation */
  70.  
  71. Widget        toplevel,       /* toplevel shell */
  72.               glw;            /* current widget */
  73.              
  74. GLXContext    glxc;           /* current glx context */
  75.  
  76. Arg args[20];
  77. int acnt;
  78.  
  79. /* coords of a cube */
  80. static GLfloat cube[8][3] = {0.0,0.0,0.0,
  81.                 1.0,0.0,0.0,
  82.                 1.0,0.0,1.0,
  83.                 0.0,0.0,1.0,
  84.                 1.0,1.0,0.0,
  85.                 1.0,1.0,1.0,
  86.                 0.0,1.0,1.0,
  87.                 0.0,1.0,0.0};
  88.  
  89. static int faceIndex[6][4] = {0,1,2,3,
  90.                   1,4,5,2,
  91.                   4,7,6,5,
  92.                   7,0,3,6,
  93.                   3,2,5,6,
  94.                   7,4,1,0};
  95. void 
  96. main(int argc, char** argv)
  97. {
  98.     XtToolkitInitialize();
  99.     appContext = XtCreateApplicationContext();
  100.     display    = XtOpenDisplay(appContext, NULL, "Ostencil","ostencil",NULL,0,
  101.                               &argc,argv);
  102.     if (!display) {
  103.         printf("%s : Unable to open display\n",argv[0]);
  104.         exit(0);
  105.     }
  106.  
  107.     printf("\n---------------------------------------------\n");
  108.     printf("OpenGL stencil example \n\n");
  109.     printf("Press:  left   button for animation\n\
  110.         middle button to turn OFF stenciling\n\
  111.         right  button to turn ON  stenciling (default)\n");
  112.  
  113.     createToplevel();             /* create widget hierarchy */
  114.     XtAppMainLoop(appContext);    /* loop forever */
  115. }
  116.  
  117.  
  118. void
  119. createToplevel(void)
  120. {
  121.     Widget frame;
  122.  
  123.     acnt = 0;
  124.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  125.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  126.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  127.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  128.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  129.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  130.     toplevel  = XtAppCreateShell("openGL stencil","ostencil",
  131.                                   applicationShellWidgetClass,
  132.                                   display,args,acnt);
  133.  
  134.     /* create a frame to hold glx widget */
  135.     frame   = XtVaCreateManagedWidget("frame", 
  136.                   xmFrameWidgetClass, toplevel, 
  137.                   NULL);
  138.  
  139.     /* create a double buffer widget, in rgb mode and manage it */
  140.     acnt = 0;
  141.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  142.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  143.     XtSetArg(args[acnt], GLwNstencilSize,        1); acnt++;
  144.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  145.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  146.     XtManageChild(glw);
  147.  
  148.     /* register callbacks */
  149.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  150.  
  151.     /* realize widget hierarchy */
  152.     XtRealizeWidget(toplevel);
  153.  
  154.     /* additional callbacks */
  155.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  156.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  157.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  158.  
  159. }
  160.  
  161. static float p0[] = {-5.0,-5.0,0.0};
  162. static float p1[] = {5.0,-5.0,0.0};
  163. static float p2[] = {5.0,5.0,0.0};
  164. static float p3[] = {-5.0,5.0,0.0};
  165.  
  166. void
  167. drawWireframe(face)
  168. int face;
  169. {
  170.     int i;
  171.     glBegin(GL_LINE_LOOP);
  172.     for (i=0;i<4;i++)
  173.         glVertex3fv((GLfloat *)cube[faceIndex[face][i]]);
  174.     glEnd();
  175. }
  176.  
  177. void
  178. drawFilled(face)
  179. int face;
  180. {
  181.     int i;
  182.     glBegin(GL_POLYGON);
  183.     for (i=0;i<4;i++)
  184.         glVertex3fv((GLfloat *)cube[faceIndex[face][i]]);
  185.     glEnd();
  186. }
  187.  
  188. void
  189. drawScene(void)
  190. {
  191.  
  192.     int i;
  193.     glEnable(GL_DEPTH_TEST);
  194.     glDepthFunc(GL_LEQUAL);
  195.  
  196.     glClearColor(0.0, 0.0, 0.0, 0.0);
  197.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  198.  
  199.     glPushMatrix();
  200.  
  201.     glRotatef (ax,1.0,0.0,0.0);
  202.     glRotatef (-ay,0.0, 1.0, 0.0);
  203.  
  204.     /* all the good stuff follows */
  205.  
  206.     if (stencilOn) { 
  207.     glEnable(GL_STENCIL_TEST);
  208.         glClear(GL_STENCIL_BUFFER_BIT);
  209.         glStencilMask(1);
  210.         glStencilFunc(GL_ALWAYS,0,1);
  211.         glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
  212.     }
  213.  
  214.     glColor3f (1.0, 1.0, 0.0);
  215.     for (i=0;i<6;i++) {
  216.     drawWireframe(i);
  217.         if (stencilOn) {
  218.             glStencilFunc(GL_EQUAL, 0, 1);
  219.             glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  220.         }
  221.  
  222.         glColor3f (0.0, 0.0, 0.0);
  223.     drawFilled(i);
  224.  
  225.         glColor3f (1.0, 1.0, 0.0);
  226.         if (stencilOn) {
  227.             glStencilFunc(GL_ALWAYS,0,1);
  228.             glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
  229.         }
  230.         glColor3f (1.0, 1.0, 0.0);
  231.     drawWireframe(i);
  232.     }
  233.     glPopMatrix();
  234.  
  235.     if (stencilOn) glDisable(GL_STENCIL_TEST);
  236.  
  237.     /* end of good stuff */
  238.  
  239.     glFlush();
  240.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  241.  
  242. }
  243.  
  244. void
  245. setMatrix(void)
  246. {
  247.     glMatrixMode(GL_PROJECTION);
  248.     glLoadIdentity();
  249.     glOrtho(-2.0,2.0,-2.0,2.0,-2.0,2.0);
  250.     glMatrixMode(GL_MODELVIEW);
  251.     glLoadIdentity();
  252. }
  253.  
  254. void
  255. animation(void)
  256. {
  257.     register int i;
  258.     /* animate the cone */
  259.  
  260.     for (i=0;i<60;i++) {
  261.         ax += 5.0;
  262.         ay -= 2.0;
  263.         az += 5.0;
  264.         if (ax >= 360)  ax = 0.0;
  265.         if (ay <= -360) ay = 0.0;
  266.         if (az >= 360)  az = 0.0;
  267.         drawScene();
  268.     }
  269. }
  270.  
  271. void 
  272. inputCB(Widget w, XtPointer client_data, XtPointer call)
  273. {
  274.     char            string[31];
  275.     XComposeStatus  composeStatus;
  276.     KeySym keysym;
  277.     GLwDrawingAreaCallbackStruct *call_data;
  278.  
  279.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  280.  
  281.     switch(call_data->event->type) {
  282.     case ButtonPress:
  283.         switch (call_data->event->xbutton.button) {
  284.         case Button1:
  285.         animation();
  286.             break;
  287.         case Button2 :
  288.         stencilOn = 0;
  289.         XtVaSetValues(toplevel,XmNtitle, "Stencil Disabled",NULL);
  290.         drawScene();
  291.             break;
  292.         case Button3 :
  293.         stencilOn = 1;
  294.         XtVaSetValues(toplevel,XmNtitle, "Stencil Enabled",NULL);
  295.         drawScene();
  296.             break;
  297.         }
  298.         break;
  299.     case KeyPress :
  300.         XLookupString((XKeyEvent *)call_data->event,string,
  301.                       30,&keysym,&composeStatus);
  302.         switch(keysym) {
  303.         case XK_Escape :
  304.             exit(0);
  305.             break;
  306.         }
  307.     break;
  308.     default:
  309.         break;
  310.     }
  311. }
  312.  
  313. void 
  314. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  315. {
  316.     GLwDrawingAreaCallbackStruct *call_data;
  317.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  318.  
  319.     GLwDrawingAreaMakeCurrent(w, glxc);
  320.     glViewport(0, 0, call_data->width, call_data->height);
  321.     setMatrix();
  322.  
  323.     drawScene();
  324. }
  325.  
  326. void 
  327. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  328. {
  329.     GLwDrawingAreaCallbackStruct *call_data;
  330.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  331.  
  332.  
  333.     GLwDrawingAreaMakeCurrent(w, glxc);
  334.     glViewport(0, 0, call_data->width, call_data->height);
  335.     drawScene();
  336. }
  337.  
  338. void
  339. initCB(Widget w, XtPointer client_data, XtPointer call)
  340. {
  341.     XVisualInfo *vi;
  342.  
  343.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  344.     XtGetValues(w, args, 1);
  345.  
  346.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  347.  
  348.     ax = 10.0;
  349.     ay = -10.0;
  350.     az = 0.0;
  351. }
  352.